package com.platform.cdcs.fragment.detail;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.InvoiceList;
import com.platform.cdcs.tool.Constant;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.sherchen.slidetoggleheader.views.XListView;
import com.trueway.app.uilib.adapter.EnhancedAdapter;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by holytang on 2017/11/3.
 */
public class InOutErrFragment extends BaseFragment {

    private ObservableXListView listView;
    private ItemAdapter adapter;
    private int pageIndex = 1;

    @Override
    public void initView(View view) {
        adapter = new ItemAdapter(getContext());
        initLoadImg(view.findViewById(R.id.load));
        hideThisToolBar(view);
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setTitle("出入库错误信息");
        listView = (ObservableXListView) view.findViewById(android.R.id.list);
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                adapter.clear();
                adapter.notifyDataSetChanged();
                requestList(false);
            }

            @Override
            public void onLoadMore() {
                requestList(false);
            }
        });
        listView.setAdapter(adapter);
        listView.setPullRefreshEnable(false);
        requestList(true);
    }

    private void requestList(boolean flag) {
        if (flag) {
            showLoadImg();
        }
        Map<String, String> param = new HashMap<>();
        param.put("fileReportId", getArguments().getString("id"));
        param.put("pageSize", String.valueOf(Constant.PAGE_SIZE));
        param.put("pageIndex", String.valueOf(pageIndex));
        getHttpClient().post().url(Constant.REPORT_ERROR_LST).params(Constant.makeParam(param)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getActivity(), R.string.server_error);

            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    if (getContext() == null) {
                        return;
                    }
                    dismissLoadImg();
                    listView.stopLoadMore();
                    listView.stopRefresh();
                    Type type = new TypeToken<BaseObjResponse<InvoiceList>>() {
                    }.getType();
                    BaseObjResponse<InvoiceList> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        adapter.addAll(response.getResult().getFileReportErrorList());
                        adapter.notifyDataSetChanged();
                        if (response.getResult().getFileReportErrorList().size() == Constant.PAGE_SIZE) {
                            pageIndex++;
                            listView.setPullLoadEnable(true);
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {

                }
            }
        });
    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    private class ItemAdapter extends EnhancedAdapter<InvoiceList.Invoice> {

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected void bindView(View paramView, Context paramContext, int position) {
            InvoiceList.Invoice invoice = getItem(position);
            ViewHolder holder = (ViewHolder) paramView.getTag();
            holder.title1.setText(invoice.getSerialNumber());
            holder.title2.setText(invoice.getItemCode());
            holder.title3.setText(invoice.getErrorIndex());
            holder.title4.setText(Double.parseDouble(invoice.getQty())+ " " + invoice.getUom());
            holder.text1.setText(invoice.getErrorDesc());
            holder.text2.setText(invoice.getDocNo());
            holder.text3.setText(invoice.getDocDate());
            holder.text4.setText(invoice.getDocType());
        }

        @Override
        protected View newView(Context context, int position, ViewGroup viewgroup) {
            View rootView = inflater.inflate(R.layout.inout_err_item, viewgroup, false);
            ViewHolder holder = new ViewHolder();
            holder.title1 = (TextView) rootView.findViewById(R.id.title);
            holder.title2 = (TextView) rootView.findViewById(R.id.title1);
            holder.title3 = (TextView) rootView.findViewById(R.id.title2);
            holder.title4 = (TextView) rootView.findViewById(R.id.title3);
            holder.text1 = (TextView) rootView.findViewById(R.id.text);
            holder.text2 = (TextView) rootView.findViewById(R.id.text1);
            holder.text3 = (TextView) rootView.findViewById(R.id.text2);
            holder.text4 = (TextView) rootView.findViewById(R.id.text3);
            rootView.setTag(holder);
            return rootView;
        }
    }

    private class ViewHolder {
        TextView title1, title2, title3, title4, text1, text2, text3, text4;
    }

}
